package webClient.UserInterfaceClasses;

import java.awt.GridLayout;

import javax.swing.JFrame;

import webClient.UserInterfaceApplet.buddyList;
import webClient.UserInterfaceApplet.clientapp;

public class WebChat {

	private webClient.UserInterfaceApplet.buddyList engine = null;
	private String convID = null;
	private clientapp window = null;
	private JFrame appletFrame = null;
	
	public WebChat(String id){
		
		convID = id;
		
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
            	window = new clientapp();
        		appletFrame = new JFrame("Chat");
        		appletFrame.setLayout(new GridLayout(1,0));
        		
        		appletFrame.resize(465, 400);
        		appletFrame.setResizable(false);
        		appletFrame.setVisible(true);
        		appletFrame.add(window);
        		appletFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        		window.setChat(WebChat.this);
        		
        		window.init();
        		window.start();
            }
        });
	}

	public void deliverMessage(String sender, String message){
		
		window.printMessage(sender, message);
		
	}
	
	public void setEngine(buddyList engine){
		
		this.engine = engine;
		
	}
	
	public String getID(){
		
		return convID;
		
	}

	public void closeChat(){

		appletFrame.dispose();
		
	}

	public void sendMessage(String message){
		
		engine.sendMessage(message, convID);
		
	}
	
	public void inviteBuddy(String buddyname){
		
		this.engine.inviteBuddy(buddyname, convID);
		
	}
}
